package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingBarracks;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingPatrons;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingTeam;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingUserDataDaoImpl extends TempDataObjectDao<CrossTongBingUserData> {

    public static String getCrossTongBingUserDataSql = "select * from `t_c_tongbing_user_data` where `activityId`=? and groupId=? ";

    public Map<Long, CrossTongBingUserData> getCrossTongBingUserDataMap(int activityId, int groupId) {
        Map<Long, CrossTongBingUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossTongBingUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTongBingUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossTongBingUserDataSql = "update `t_c_tongbing_user_data` set unionUid=?,campId=?,patronsMap=?,soldierAddition=?,teamMap=?,barracks=?,eventResetTime=?,eventTodayCount=?,eventMap=?,positionId=?,posAddSilverMap=?,posAddSilverTime=?,zhanGong=?,zhanGongTime=?,needHelpTime=?,redDotMap=?,horseId=?,guide=?,totalPosSilver=? where activityId=? and groupId=? and userId=?";

    @Override
    protected boolean updateData(CrossTongBingUserData crossTongBingUserData) {
        crossTongBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getSoldierAddition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getEventResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getEventTodayCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getPosAddSilverMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getPosAddSilverTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getNeedHelpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUserData.getTotalPosSilver().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossTongBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossTongBingUserDataSql = "insert into `t_c_tongbing_user_data`(activityId,groupId,userId,unionUid,campId,patronsMap,soldierAddition,teamMap,barracks,eventResetTime,eventTodayCount,eventMap,positionId,posAddSilverMap,posAddSilverTime,zhanGong,zhanGongTime,needHelpTime,redDotMap,horseId,guide,totalPosSilver) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossTongBingUserData crossTongBingUserData) {
        crossTongBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getSoldierAddition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getEventResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getEventTodayCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getPosAddSilverMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getPosAddSilverTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUserData.getNeedHelpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUserData.getGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUserData.getTotalPosSilver().toString()));
        if (getDb().executeNoneQuery(insertCrossTongBingUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossTongBingUserData parseData(ResultSet rs) throws SQLException {
        CrossTongBingUserData crossTongBingUserData = new CrossTongBingUserData();
        crossTongBingUserData.setActivityId(rs.getInt("activityId"));
        crossTongBingUserData.setGroupId(rs.getInt("groupId"));
        crossTongBingUserData.setUserId(rs.getLong("userId"));
        crossTongBingUserData.setUnionUid(rs.getString("unionUid"));
        crossTongBingUserData.setCampId(rs.getInt("campId"));
        crossTongBingUserData.setPatronsMap(JSON.parseObject(rs.getString("patronsMap"), new TypeReference<ConcurrentHashMap<Integer, CrossTongBingPatrons>>() {
        }));
        crossTongBingUserData.setSoldierAddition(JSON.parseObject(rs.getString("soldierAddition"), new TypeReference<List<Long>>() {
        }));
        crossTongBingUserData.setTeamMap(JSON.parseObject(rs.getString("teamMap"), new TypeReference<ConcurrentHashMap<Integer, CrossTongBingTeam>>() {
        }));
        crossTongBingUserData.setBarracks(JSON.parseObject(rs.getString("barracks"), CrossTongBingBarracks.class));
        crossTongBingUserData.setEventResetTime(rs.getLong("eventResetTime"));
        crossTongBingUserData.setEventTodayCount(rs.getLong("eventTodayCount"));
        crossTongBingUserData.setEventMap(JSON.parseObject(rs.getString("eventMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        crossTongBingUserData.setPositionId(rs.getInt("positionId"));
        crossTongBingUserData.setPosAddSilverMap(JSON.parseObject(rs.getString("posAddSilverMap"), new TypeReference<ConcurrentHashMap<Long, BigInteger>>() {
        }));
        crossTongBingUserData.setPosAddSilverTime(rs.getLong("posAddSilverTime"));
        crossTongBingUserData.setZhanGong(rs.getLong("zhanGong"));
        crossTongBingUserData.setZhanGongTime(rs.getLong("zhanGongTime"));
        crossTongBingUserData.setNeedHelpTime(rs.getLong("needHelpTime"));
        String redDotMap = rs.getString("redDotMap");
        if (!StringUtils.isNullOrEmpty(redDotMap)) {
            crossTongBingUserData.setRedDotMap(JSON.parseObject(redDotMap, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        crossTongBingUserData.setHorseId(rs.getInt("horseId"));
        crossTongBingUserData.setGuide(rs.getInt("guide"));
        String totalPosSilver = rs.getString("totalPosSilver");
        if (!StringUtils.isNullOrEmpty(totalPosSilver)) {
            crossTongBingUserData.setTotalPosSilver(new BigInteger(totalPosSilver));
        }
        return crossTongBingUserData;
    }

}
